﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace AutoDeploy.FrontEnd
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public AutoDeploy.Common.Library.DeploymentObject ado = new Common.Library.DeploymentObject();
        ObservableCollection<AutoDeploy.Common.Library.CommandValuesView> oc = new ObservableCollection<Common.Library.CommandValuesView>();
        public MainWindow()
        {
            InitializeComponent();
            butRunCommands.IsEnabled = false;
        }
        public void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
        /// <summary>
        /// Loads ComboBox from XML.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butRun_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cmbDeploymentObject.ItemsSource = AutoDeploy.Common.Library.DeployHelper.GetDeploymentObjectNames(tbAutoDeployFile.Text);
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Problem loading the ado" + ex.Message);
            }
        }
        /// <summary>
        /// Sets all the values on the page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbDeploymentObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ado = AutoDeploy.Common.Library.DeployHelper.LoadDeploymentObjectFromAutoDeployConfig(tbAutoDeployFile.Text.Trim(), cmbDeploymentObject.SelectedValue.ToString());
                dgvDeploymentObject.ItemsSource = new ObservableCollection<AutoDeploy.Common.Library.CommandSet>(AutoDeploy.Common.Library.DeployHelper.GetCommandSets(ado));
                oc = new ObservableCollection<AutoDeploy.Common.Library.CommandValuesView>(AutoDeploy.Common.Library.DeployHelper.LoadCommandValuesView(ado));
                dgvCommandValues.ItemsSource = oc;
                butRunCommands.IsEnabled = AutoDeploy.Common.Library.DeployHelper.ValidateDeploymentObject(ref ado);
                if (!butRunCommands.IsEnabled)
                {
                    MessageBox.Show("The Deployment Object Did not validate.\n" +
                        "Priority Rules:\n" +
                        "\tStarting at 1, there must be one and only one command per priority level.\n");
                }
            }
            catch (IRDev.Library.ExpandedException.Common.ExpandedException ex)
            {
                MessageBox.Show(ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Problem loading the Deployment Object {0}",ex.Message));
            }
        }
        /// <summary>
        /// Opens the Run Window. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butRunCommands_Click(object sender, RoutedEventArgs e)
        {
            EachStep es = new EachStep(oc, ado);
            es.Show();
        }
    }
}
